package cn.skyclass.controls
{
	import mx.collections.ArrayCollection;
	import mx.controls.DataGrid;
	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.core.mx_internal;
	
	use namespace mx_internal;
	
	/**
	 * 用来显示序号的列
	 */
	public class IndexDataGridColumn extends DataGridColumn
	{
		public function IndexDataGridColumn()
		{
			setStyle("textAlign", "center");
			this.headerText = "序号";
			this.labelFunction = this.labelFunctionIndex;
		}

		/**
		 * 序号的偏移量
		 */
		public var indexOffset:int = 0;
		
		/**
		 * 第一个序号的标签，如不提供此值，则使用数字 1
		 */
		public var firstIndexLabel:String;
		
		/**
		 * 最后一个序号的标签，如不提供此值，则使用对应的数字
		 */
		public var lastIndexLabel:String;
		
		/**
		 * 默认的标签，当找不到合法的序号时使用此值
		 */
		public var defaultLabel:String = "";
		
		
		protected function labelFunctionIndex(item:Object, column:DataGridColumn):String{
			var dg:DataGrid = owner as DataGrid;
			if(dg){
				var dp:ArrayCollection = dg.dataProvider as ArrayCollection;
				if(dp){
					var i:int = dp.getItemIndex(item);
					if((i == 0) && (firstIndexLabel != null)){
						return firstIndexLabel;
					}else if((i == dp.length - 1) && (lastIndexLabel != null)){
						return lastIndexLabel;
					}
					return (i + 1 + indexOffset).toString();
				}
			}
			return defaultLabel;
		}
			
	}
}